#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Time        : 2021/12/25 09:41
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : win.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
import pydicom

# 读取dicom图像
path = "../experiment_fig/I1700000"
im = pydicom.dcmread(path)
# 获取窗宽和窗位
win_width = im.WindowWidth
win_center = im.WindowCenter
# 获取像素数据，pydicom读入的dicom图像是灰度值
pixel_array = im.pixel_array
# 获取斜率和截距
slope = im.RescaleSlope
intercept = im.RescaleIntercept
# 计算CT值范围
ct_low = win_center - win_width / 2
ct_high = win_center + win_width / 2
# 方法一：CT值转灰度值
gray_low = (ct_low - intercept) / slope
gray_high = (ct_high - intercept) / slope
# 方法二：灰度值转CT值
ct_im = pixel_array * slope + intercept
print("窗宽:{}\n窗位:{}\n斜率:{}\n截距:{}\nCT值范围:[{},{}]\n灰度值范围:[{},{}]".format(
    win_width, win_center, slope, intercept, ct_low, ct_high, gray_low, gray_high
))
